package Day017_缓冲流_转换流_序列化流_打印流.demo01_缓冲流;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;

/**
 * @author zhangyan
 * @date 2020/5/9 10:32
 */
public class Test3_案例 {
    public static void main(String[] args) throws Exception{

        //1.创建ArrayList集合 存储独到的数据
        ArrayList<String> list= new ArrayList<>();

        //2.创建缓冲输入流对象
        BufferedReader br = new BufferedReader(new FileReader("Demo02\\aaa\\c.txt"));
        //BufferedWriter bw = new BufferedWriter(new FileWriter("Demo02\\aaa\\c.txt"));  不能放在这,会清空文件

        //3.循环读 定义String变量
        String line;
        while ((line = br.readLine()) != null){
            //4.读取的数据 添加到集合
            list.add(line);
        }
        br.close();

        //5.对集合进行排序
        Collections.sort(list);//升序排序
        // Comparable compareTo()方法 字典排序

        //6.创建字符缓冲输出流
        BufferedWriter bw = new BufferedWriter(new FileWriter("Demo02\\aaa\\c.txt"));

        //7.对排序的集合进行遍历
        for (String s : list) {
            //8.把遍历的行数据 写到文件
            bw.write(s);
            bw.newLine();
        }
        bw.close();
    }
}
